var bmap = require('../../../lib/map/bmap-wx.min.js');
var wxMarkerData = [];
var BMap = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [],
    latitude: '',
    longitude: '',
    placeData: {},
    search_con: '餐饮',
    icon_bar: 1,
    icon_marker_img: '../../../assets/img/index/icon_food_white@2x.png',
    location: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      location: options.lat + ',' + options.lon
    });
    var that = this;
    // 新建百度地图对象 
    BMap = new bmap.BMapWX({
      ak: 'hNxdqxCmkcnteMMijD4EMqVgp9ORGhdo'
      //hNxdqxCmkcnteMMijD4EMqVgp9ORGhdo
    });
    this.BMapsearch()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  makertap: function (e) {
    var that = this;
    var id = e.markerId;
  },
  showSearchInfo: function (data, i) {
    var that = this;
    that.setData({
      placeData: {
        title: '名称：' + data[i].title + '\n',
        address: '地址：' + data[i].address + '\n',
        telephone: '电话：' + data[i].telephone
      }
    });
  },
  BMapsuccess: function (data) {
    var that = this;
    var newWxMarkerData = data.wxMarkerData.map((val) => {
      val.callout = {
        fontSize: 13,
        color: '#FFFFFF',
        bgColor: '#FF7300',
        content: val.title,
        padding: 4
      }
      return val;
    });
    var wxMarkerData = newWxMarkerData;
    that.setData({
      markers: wxMarkerData
    });
    that.setData({
      latitude: wxMarkerData[0].latitude
    });
    that.setData({
      longitude: wxMarkerData[0].longitude
    });
  },
  BMapsearch: function () {
    var that = this;
    BMap.search({
      location: that.data.location,
      "query": that.data.search_con,
      success: that.BMapsuccess,
      width: 40,
      height: 40,
      //此处需要在相应路径放置图片文件 
      iconPath: that.data.icon_marker_img
      //此处需要在相应路径放置图片文件 
      //iconTapPath: '../../img/marker_red.png'
    });
  },
  //点击map搜索
  click_map: function (e) {
    var is_bar = e.currentTarget.dataset.tab;
    if (is_bar === 'food') {
      this.setData({
        icon_marker_img: '../../../assets/img/index/icon_food_white@2x.png',
        search_con: '餐饮',
        icon_bar: 1
      });
      this.BMapsearch();

    } else if (is_bar === 'shop') {
      this.setData({
        icon_marker_img: '../../../assets/img/index/icon_shopping_white@2x.png',
        search_con: '购物',
        icon_bar: 2
      });
      this.BMapsearch();

    } else if (is_bar === 'jiu') {
      this.setData({
        icon_marker_img: '../../../assets/img/index/icon_community_white@2x.png',
        search_con: '酒店',
        icon_bar: 3
      });
      this.BMapsearch();

    } else if (is_bar === 'qu') {
      console.log(is_bar);
      this.setData({
        icon_marker_img: '../../../assets/img/index/icon_hotel_white@2x.png',
        search_con: '小区',
        icon_bar: 4
      });
      this.BMapsearch();

    } else if (is_bar === 'school') {
      this.setData({
        icon_marker_img: '../../../assets/img/index/icon_school_white@2x.png',
        search_con: '学校',
        icon_bar: 5
      });
      this.BMapsearch();

    } else if (is_bar === 'traffic') {
      this.setData({
        icon_marker_img: '../../../assets/img/index/icon_traffic_white@2x.png',
        search_con: '地铁',
        icon_bar: 6
      });
      this.BMapsearch();
    }
  }
})